package com.itboye.jinpuyiyuan.activity.message;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/1/16 0016.
 */

public class ModifyRemarkActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private TextView real_ok;
    public static int MOVEREMARK=26;
    private EditText fankui_et;
    private PopupWindow deletePop;
    private int id;
    private String illUid;
    private BranchAddressPresenter presenter;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_modifyremark);
        toobar.setBackgroundColor(getResources().getColor(R.color.white));
        leftTv.setVisibility(View.GONE);
        fankui_et.setText(getIntent().getStringExtra("content"));
//        intent.putExtra("illId",illUid);
        illUid=getIntent().getIntExtra("illId",0)+"";
        id=getIntent().getIntExtra("tag_id",0);
        presenter=new BranchAddressPresenter(this);
        leftImg.setImageDrawable(getResources().getDrawable(R.drawable.fanhuijian));
        rightImg.setImageDrawable(getResources().getDrawable(R.drawable.shanchu_remark));
        tvTitle.setText("修改备注");
        tvTitle.setTextColor(getResources().getColor(R.color.title_blue));
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

            case R.id.rightImg:
                showDeletePopwindow();
                break;
            case R.id.real_ok:
                if (fankui_et.getText().toString().equals("")){
                    MAlert.alert("标签内容不能为空");
                    return;
                }
                presenter.updateRemark(id+"",illUid,fankui_et.getText().toString());
                break;
        }
    }

    public void showDeletePopwindow(){

        View inflate = LayoutInflater.from(this).inflate(R.layout.popwind_modifyremark, null);
        deletePop=new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

        TextView cancel = inflate.findViewById(R.id.cancel);
        TextView delete = inflate.findViewById(R.id.delete);
        deletePop.setAnimationStyle(R.style.popupwindow_anim_style);
        deletePop.setFocusable(true);
        inflate.setFocusableInTouchMode(true); //设置view能够接听事件

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               deletePop.dismiss();
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //确认后删除标签
                presenter.removeRemark(id);
                deletePop.dismiss();
            }
        });

        //点击空白处取消
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                deletePop.dismiss();
            }
        });
        //设置findview完后
        deletePop.showAtLocation(inflate, Gravity.TOP,0,0);
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType() ==BranchAddressPresenter.By_UserTags_delete_success){
//                MAlert.alert("删除成功");
                setResult(ModifyRemarkActivity.MOVEREMARK);
                this.finish();
            }else if (resultEntity.getEventType() ==BranchAddressPresenter.By_UserTags_delete_fail){

//                MAlert.alert("删除失败");
            }else if (resultEntity.getEventType() == BranchAddressPresenter.By_UserTags_edit_success){
                MAlert.alert("修改成功");
                setResult(ModifyRemarkActivity.MOVEREMARK);
                this.finish();
            }else if (resultEntity.getEventType() == BranchAddressPresenter.By_UserTags_edit_fail){
                MAlert.alert(resultEntity.getMsg());
            }
        }
    }
}
